package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.ImagesFileMapper;
import com.gxa.insurance.mapper.OrderMapper;
import com.gxa.insurance.mapper.RemoteOrderMapper;
import com.gxa.insurance.service.RemoteOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @ClassName RemoteOrderServiceImpl
 * @Description TODO
 * @Author ds
 * @Date 2022/6/13 16:31
 * @Version 1.0
 */

@Service
@Transactional()
public class RemoteOrderServiceImpl implements RemoteOrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    RemoteOrderMapper remoteOrderMapper;

    @Autowired
    ImagesFileMapper imagesFileMapper;

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void update(Map map) throws Exception {

        Object orderId = map.get("orderId");
        Integer id = Integer.valueOf(String.valueOf(orderId));
        String orderStatus = (String)map.get("orderStatus");
        List<Integer> status = orderMapper.selectStatus(orderStatus);
        Integer statusId = status.get(0);
        Integer integer = orderMapper.updateOrderStatus(statusId,id);
        if(integer==0){
            throw new BusinessException(ExceptionCode.UPDATE_FILED);
        }

        String images =(String) map.get("images");
        String[] split = images.split(",");
        for (int i = 0; i < split.length; i++) {
            Integer insert = imagesFileMapper.insert(id, split[i]);
        }

        String isMake = (String)map.get("isMakeCompensation");
        Boolean isMakeCompemsation = Boolean.valueOf(isMake);
        map.put("isMakeCompemsation",isMakeCompemsation);
        String isSend = (String)map.get("isSendMessage");
        Boolean isSendMessage = Boolean.valueOf(isSend);
        map.put("isSendMessage",isSendMessage);
        Integer update = remoteOrderMapper.update(map);
        if(integer==0){
            throw new BusinessException(ExceptionCode.UPDATE_FILED);
        }

    }
}
